from typing import List
from common.enum.strategy_group_enum import StrategyGroup
from common.enum.strategy_type_enum import StrategyType
from dto.strategy_assessment import RiskAssessment
from dto.strategy_info import StrategyInfoMetadata
from dto.strategy_stock_day import StrategyStockDay
from service.strategy.base_strategy import BaseStrategy


class ThreeDaysDownStrategy(BaseStrategy):
    """
    连续3天下跌策略，每天下跌得分 +1，如果当天是最低价收盘得分 +2
    """

    def analyze(self, trade_info_list: List[StrategyStockDay]) -> RiskAssessment:
        """
        分析连续3天下跌的股票，如果满足条件则返回评分
        """
        node_point = 0
        description = ""

        # 检查是否至少有3天的交易数据
        if len(trade_info_list) >= 3:
            # 遍历最近三天的数据
            for i in range(2, len(trade_info_list)):
                # 当前天和前一天的收盘价比较，若下跌则得分 +1
                if trade_info_list[i].close < trade_info_list[i - 1].close:
                    node_point += 1
                    description += f"Day {i}: Stock decreased (score +1). "

                    # 判断是否是最低价收盘
                    if trade_info_list[i].close == trade_info_list[i].low:
                        node_point += 2  # 最低价收盘得分 +2
                        description += f"Day {i}: Closed at lowest price (score +2). "

        return RiskAssessment(
            stock_code=trade_info_list[0].stock_code,
            description=description,
            config=self.strategyConfig(),
            node_point=node_point,
        )

    def strategyConfig(self) -> StrategyInfoMetadata:
        """
        返回策略的配置
        """
        return StrategyInfoMetadata(
            strategy_code="three_days_down",
            strategy_name="连续多天下跌",
            strategy_group=StrategyGroup.RISK,
            strategy_type=StrategyType.TIME_POINT,
            analysis_day=3,
            strategy_level=2,
        )
